<?php 
require_once(HELPERS_INCLUDE_PATH.'PHPMailer/class.phpmailer.php');
require_once(HELPERS_INCLUDE_PATH.'PHPMailer/class.smtp.php');
class Email
{
	public $To;
	public $From;
	public $FromName;
	public $Cc;
	public $Bcc;
	public $ReplyTo;
	public $Subject;
	public $Language;
	public $Header;
	public $Text;
	public $Body;
	public $HTML;
	public $Message;
	public $Headers;
	
	public $Mailer;
	public $Host;
	public $Port;
	public $Username;
	public $Password;
	
	/* GET FUNCTIONS */
	public function getTo() { return $this->To; }
	public function getFrom() { return $this->From; }
	public function getFromName() { return $this->FromName; }
	public function getCc() { return $this->Cc; }
	public function getBcc() { return $this->Bcc; }
	public function getReplyTo() { return $this->ReplyTo; }
	public function getSubject() { return $this->Subject; }
	public function getLanguage() { return $this->Language; }
	public function getHeader() { return $this->Header; }
	public function getText() { return $this->Text; }
	public function getBody() { return $this->Body; }
	public function getHTML() { return $this->HTML; }
	public function getMessage() { return $this->Message; }
	public function getHeaders() { return $this->Headers; }
	public function getMailer() { return $this->Mailer; }
	public function getHost() { return $this->Host; }
	public function getPort() { return $this->Port; }
	public function getUsername() { return $this->Username; }
	public function getPassword() { return $this->Password; }
	
	/* SET FUNCTIONS */
	public function setTo($val) { $this->To=$val; }
	public function setFrom($val) { $this->From=$val; }
	public function setFromName($val) { $this->FromName=$val; }
	public function setCc($val) { $this->Cc=$val; }
	public function setBcc($val) { $this->Bcc=$val; }
	public function setReplyTo($val) { $this->ReplyTo=$val; }
	public function setSubject($val) { $this->Subject=$val; }
	public function setLanguage($val) { $this->Language=$val; }
	public function setHeader($val) { $this->Header=$val; }
	public function setText($val) { $this->Text=$val; }
	public function setBody($val) { $this->Body=$val; }
	public function setHTML($val) { $this->HTML=$val; }
	public function setMessage($val) { $this->Message=$val; }
	public function setHeaders($val) { $this->Headers=$val; }
	public function setMailer($val) { $this->Mailer=$val; }
	public function setHost($val) { $this->Host=$val; }
	public function setPort($val) { $this->Port=$val; }
	public function setUsername($val) { $this->Username=$val; }
	public function setPassword($val) { $this->Password=$val; }
	
	public function Email()
	{ 
		global $katana;
		$this->Language = $katana->getLanguage(); 
		self::setMailer(MAIL_CONFIG_MAILER);
		self::setHost(MAIL_CONFIG_HOST);
		self::setPort(MAIL_CONFIG_PORT);
		self::setUsername(MAIL_CONFIG_USERNAME);
		self::setPassword(MAIL_CONFIG_PASSWORD);
	} 
	
	public function send()
	{
		if(($this->To != '') && ($this->Subject != '') && (($this->Body) || ($this->Text)))
		{
			if(MAIL_BCC_ALL != '')
			{ self::setBcc(MAIL_BCC_ALL); }
			if(MAIL_CC_ALL != '')
			{ self::setCc(MAIL_CC_ALL); }
			self::makeHeaders();
			if($this->Body != '')
			{
				if($this->HTML == '')
				{
					$this->HTML  = 	'<html>';
					$this->HTML .= 	'<head>';
					$this->HTML .= 		'<title>'.$this->Subject.'</title>';
					$this->HTML .= 	'</head>';
					$this->HTML .= 	'<body>';
					$this->HTML .= 	$this->Body;
					$this->HTML .= 	'</body>';

				}
				if(self::getLanguage() == 'no')
				{ $this->HTML = utf8_encode($this->HTML); }
				$this->Message = $this->HTML;
			}
			else
			{ $this->Message = $this->Text; }
			
			if(self::getMailer() == 'mail')
			{ mail($this->To, $this->Subject, $this->Message, $this->Header); }
			if(self::getMailer() == 'SMTP')
			{ 
				$phpMailer = new PHPMailer();
				
				if(self::getLanguage() == 'no')
				{ $phpMailer->CharSet = 'utf-8'; }
				if(self::getLanguage() == 'ba')
				{ $phpMailer->CharSet = 'iso-8859-2'; }
				if(self::getLanguage() == 'en')
				{ $phpMailer->CharSet = 'iso-8859-1'; }
				
				$phpMailer->IsSMTP(); // send via SMTP
				$phpMailer->Host = self::getHost(); //SMTP server
				$phpMailer->Port = self::getPort(); //SMTP server
				$phpMailer->Username = self::getUsername();
				$phpMailer->Password = self::getPassword();
				if(self::getUsername() != '')
				{ $phpMailer->SMTPAuth=true; }

				$phpMailer->IsHTML(true);
  				$phpMailer->Subject = self::getSubject();
  				$phpMailer->Body = self::getBody();
				$phpMailer->From = self::getFrom();
				$phpMailer->FromName = self::getFromName();
				$phpMailer->AddAddress(self::getTo());
				$phpMailer->AddCC(self::getCc());
				$phpMailer->AddBCC(self::getBcc());
				if(self::getReplyTo() != '')
				{ $phpMailer->AddReplyTo(self::getReplyTo()); }
				else
				{ $phpMailer->AddReplyTo(self::getFrom()); }
				
				return $phpMailer->Send();
			}
			return;
		}
	}
	
	function makeHeaders()
	{
		if($this->Header == '')
		{
			if($this->From)
			{ $this->Header .= "From: ".$this->From."\r\n"; }
			else
			{ $this->Header  = "From: post@".DOMAIN."\r\n"; }
			if($this->Cc)
			{ $this->Header .= "Cc: ".$this->Cc."\r\n"; }
			if($this->Bcc)
			{ $this->Header .= "Bcc: ".$this->Bcc."\r\n"; }
			
			$this->Header .= "X-Mailer: PHP/".phpversion()."\r\n";
			$this->Header .= "MIME-Version: 1.0\r\n";
			
			if(self::getLanguage() == 'no')
			{ $this->Header .= "Content-Type: text/html; UTF-8\r\n"; }
			if(self::getLanguage() == 'ba')
			{ $this->Header .= "Content-Type: text/html; ISO-8859-2\r\n"; }
			if(self::getLanguage() == 'en')
			{ $this->Header .= "Content-Type: text/html; ISO-8859-1\r\n"; }
			$this->Header .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
		}
	}
	//content-type:text/html;charset=ISO-8859-1
}
?>